package home.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;

import net.ck.hzq.MyEarningsActivity;
import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import dao.HomeBenefitBean;
import util.ToastUtils;

public class BenefitAdapter extends DelegateAdapter.Adapter<BenefitAdapter.BenefitVH> {

    private final Context mContext;
    private HomeBenefitBean mData;

    public BenefitAdapter(Context context) {
        mContext = context;
    }

    public void setupData(HomeBenefitBean data) {
        mData = data;
        notifyDataSetChanged();
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new SingleLayoutHelper();
    }

    @NonNull
    @Override
    public BenefitVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new BenefitVH(LayoutInflater.from(mContext).inflate(R.layout.item_home_benefit, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BenefitVH holder, int i) {
        if (mData == null) {
            return;
        }
        holder.totalAmount.setText(mData.syze);
        holder.cashBackAmount.setText(mData.fxje);
        holder.profitAmount.setText(mData.frje);
        holder.otherAmount.setText(mData.qtje);
        holder.more.setOnClickListener(v -> {
            if (!TobuyApplication.isAuthed()) {
                ToastUtils.shortToast("请先进行实名认证");
                return;
            }
            Intent intent = new Intent(mContext, MyEarningsActivity.class);
            mContext.startActivity(intent);
        });
        holder.totalAmount.setOnClickListener(v -> {
            if (!TobuyApplication.isAuthed()) {
                ToastUtils.shortToast("请先进行实名认证");
                return;
            }
            Intent intent = new Intent(mContext, MyEarningsActivity.class);
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    class BenefitVH extends RecyclerView.ViewHolder {
        private View more;
        private TextView totalAmount;
        private TextView cashBackAmount;
        private TextView profitAmount;
        private TextView otherAmount;

        BenefitVH(@NonNull View itemView) {
            super(itemView);
            more = itemView.findViewById(R.id.more);
            totalAmount = itemView.findViewById(R.id.total_amount);
            cashBackAmount = itemView.findViewById(R.id.cash_back_amount);
            profitAmount = itemView.findViewById(R.id.profit_amount);
            otherAmount = itemView.findViewById(R.id.other_amount);
        }
    }

}
